package view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import servidor.ObservadorServidor;
import servidor.Servidor;

public class Gui extends JFrame implements Iu, ObservadorServidor, ActionListener{
	
	private JTextArea taMaquinasConectadas;

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private Servidor servidor;
	private JLabel lEstado, lPorta;
	private JButton btnIniciar, btnParar;
	private JTextArea taRequisicoes;

	public Gui(Servidor servidor) {
		this.servidor = servidor;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 900, 700);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 173, 183, 307);
		contentPane.add(scrollPane);
		
		taMaquinasConectadas = new JTextArea();
		scrollPane.setViewportView(taMaquinasConectadas);
		
		JLabel lblPorta = new JLabel("Porta");
		lblPorta.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblPorta.setBounds(10, 93, 57, 14);
		contentPane.add(lblPorta);
		
		lPorta = new JLabel("0");
		lPorta.setBounds(84, 93, 109, 14);
		contentPane.add(lPorta);
		
		JLabel lblIp = new JLabel("Ip");
		lblIp.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblIp.setBounds(10, 109, 46, 28);
		contentPane.add(lblIp);
		
		JLabel lIp = new JLabel("0");
		lIp.setBounds(84, 118, 109, 14);
		contentPane.add(lIp);
		
		JLabel lblMquinasConectadas = new JLabel("M\u00E1quinas conectadas");
		lblMquinasConectadas.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblMquinasConectadas.setHorizontalAlignment(SwingConstants.CENTER);
		lblMquinasConectadas.setBounds(10, 148, 183, 24);
		contentPane.add(lblMquinasConectadas);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(203, 23, 671, 489);
		contentPane.add(scrollPane_1);
		
		taRequisicoes = new JTextArea();
		scrollPane_1.setViewportView(taRequisicoes);
		
		JLabel lblRequisies = new JLabel("Requisi\u00E7\u00F5es");
		lblRequisies.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblRequisies.setBounds(203, 0, 133, 21);
		contentPane.add(lblRequisies);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(10, 523, 559, 128);
		contentPane.add(scrollPane_2);
		
		JTextArea taLogs = new JTextArea();
		scrollPane_2.setViewportView(taLogs);
		
		JLabel lblLog = new JLabel("Logs");
		lblLog.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblLog.setBounds(10, 488, 46, 24);
		contentPane.add(lblLog);
		
		JLabel lblEstado = new JLabel("Estado");
		lblEstado.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblEstado.setBounds(10, 68, 57, 14);
		contentPane.add(lblEstado);
		
		lEstado = new JLabel("Offline");
		lEstado.setBounds(84, 68, 109, 14);
		contentPane.add(lEstado);
		
		btnIniciar = new JButton("Iniciar");
		btnIniciar.setBounds(0, 11, 89, 23);
		contentPane.add(btnIniciar);		
		btnIniciar.addActionListener(this);
		
		btnParar = new JButton("Parar");
		btnParar.setBounds(104, 11, 89, 23);
		contentPane.add(btnParar);
		btnParar.addActionListener(this);
		
		setVisible(true);
	}

	@Override
	public void setEstado(boolean estado) {		
		
	}

	@Override
	public void setPorta(int porta) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setIp(String ip) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setMaquinaConectada(String idMaquina) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setRequisicao(String requisicao) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setLog(String mensagem) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void servidorAtualizado(Servidor servidor) {
		atualizarEstado();
		atualizarPorta();
//		atualizarRequisicoes();
		repaint();
	}

//	private void atualizarRequisicoes() {
//		Requisicao r = servidor.getRequisicaoAtualizada();
//		if(r!=null){
//			taRequisicoes.setText(taRequisicoes.getText().toString()+"/n"+r.toString());
//			
//		}
//		taRequisicoes.setText("rss");
//	}

	private void atualizarPorta() {
		lPorta.setText(servidor.getPorta()+" ");		
	}

	private void atualizarEstado() {
		if(servidor.isAtivo()){
			lEstado.setText("Online");
			
		}else{
			lEstado.setText("Offline");
		}		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnIniciar){
			Thread threadServidor = new Thread(servidor);
			threadServidor.start();
		}
		if(e.getSource() == btnParar){
			servidor.setAtivo(false);
		}		
	}
}
